package com.joker.web.controller.orange;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.joker.common.core.domain.PageQuery;
import com.joker.common.core.domain.R;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.helper.LoginHelper;
import com.joker.orange.domain.PlayRecord;
import com.joker.orange.domain.bo.*;
import com.joker.orange.domain.vo.*;
import com.joker.orange.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 短视频相关
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/short/video")
public class AppShortVideoController {

    private final IVideoInfoService videoInfoService;
    private final IAdvertisingSpaceService advertisingSpaceService;
    private final IShortVideoAdvertisingService shortVideoAdvertisingService;
    private final IPlayRecordService playRecordService;

    /**
     * 短视频首页
     */
    @PostMapping("/selectPageShortVideoList")
    public TableDataInfo<ShortVideoInfoVo> selectPageShortVideoList(@RequestBody ShortVideoInfoBo bo) {
        return videoInfoService.selectPageShortVideoList(bo);
    }

    /**
     * 短视频广告
     */
    @PostMapping("/selectShortVideoAd")
    public R<ShortVideoAdvertisingVo> selectShortVideoAd() {
        ShortVideoAdvertisingBo shortVideoAdvertisingBo = new ShortVideoAdvertisingBo();
        shortVideoAdvertisingBo.setIsDisplay("0");
        List<ShortVideoAdvertisingVo> advertisingList = shortVideoAdvertisingService.queryList(shortVideoAdvertisingBo);
        // 创建一个Random对象
        Random random = new Random();
        // 生成一个介于0（包括）和列表大小（不包括）之间的随机索引
        int randomIndex = random.nextInt(advertisingList.size());
        // 根据随机索引从列表中获取元素
        ShortVideoAdvertisingVo randomElement = advertisingList.get(randomIndex);
        return R.ok(randomElement);
    }

    /**
     * 短视频观看
     * videoId 视频ID
     */
    @PostMapping("/lookVideoById")
    public R<Void> lookVideoById(@RequestBody JSONObject jsonObject) {
        if (ObjectUtil.isNull(jsonObject.get("videoId"))) {
            return R.fail("视频ID不能为空");
        }
        VideoInfoBo bo = new VideoInfoBo();
        bo.setId(jsonObject.getLong("videoId"));

        VideoInfoVo videoInfoVo = videoInfoService.queryById(bo.getId());
        bo.setLookNum(videoInfoVo.getLookNum() + 1);
        videoInfoService.updateByBo(bo);
        // 新增播放记录
        PlayRecordBo playRecordBo = new PlayRecordBo();
        playRecordBo.setVideoId(videoInfoVo.getId());
        playRecordBo.setUserId(LoginHelper.getUserId());
        playRecordBo.setVideoType("1");
        PlayRecord playRecord = playRecordService.queryPlayRecord(playRecordBo);
        if (playRecord == null) {
            playRecordService.insertByBo(playRecordBo);
        }
        return R.ok();
    }

    /**
     * 查询短视频用户列表
     * shortVideoUserId 短视频用户ID
     * pageNum 当前页数
     * pageSize 分页大小
     */
    @PostMapping("/selectPageShortVideoUserList")
    public R<Map<String, Object>> selectPageShortVideoUserList(@RequestBody JSONObject jsonObject) {
        Map<String, Object> result = new HashMap<>();
        if (ObjectUtil.isNull(jsonObject.get("shortVideoUserId"))) {
            return R.fail("短视频用户ID为空");
        }

        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(jsonObject.getInt("pageNum"));
        pageQuery.setPageSize(jsonObject.getInt("pageSize"));
        VideoInfoBo bo = new VideoInfoBo();
        bo.setShortVideoUserId(jsonObject.getLong("shortVideoUserId"));
        TableDataInfo<VideoInfoVo> dataInfo = videoInfoService.queryPageList(bo, pageQuery);
        result.put("rows", dataInfo.getRows());
        result.put("total", dataInfo.getTotal());
        List<VideoInfoVo> videoInfoVos = videoInfoService.queryList(bo);
        // 使用Stream API计算likeNum的总和
        int totalLikes = videoInfoVos.stream()
                .mapToInt(VideoInfoVo::getLikeNum)
                .sum();
        result.put("totalLikes", totalLikes);
        int totalCollects = videoInfoVos.stream()
                .mapToInt(VideoInfoVo::getCollectNum)
                .sum();
        result.put("totalCollects", totalCollects);
        return R.ok(result);
    }

}
